package com.zjitc.controller;

import com.zjitc.common.ServerResponse;
import com.zjitc.pojo.Product;
import com.zjitc.pojo.User;
import com.zjitc.service.ProductService;
import com.zjitc.vo.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * Create By IntelliJ IDEA
 *
 * @author: myq
 * @create-Time: 2017/11/15  14:51
 */
@Controller
@RequestMapping("/product")
public class ProductController extends BaseController {

  private final int COUNT = 12;

  @Autowired
  private ProductService service;

  @RequestMapping(value = "/displaymsg", produces = "application/json;charset=UTF-8")
  public ModelAndView displaymsg(HttpSession session, String pid)
      throws ServletException, IOException {
    return service.displaymsg(session, pid);
  }

  @RequestMapping("/list")
  public ModelAndView list(@RequestParam(value = "cid",required = false,defaultValue = "") String cid,
                           @RequestParam(value = "pageid", required = false, defaultValue = "1") Integer pageid,
                           HttpSession session) throws ServletException, IOException {
    return service.initProductList(cid,pageid,session,COUNT);
  }


  @RequestMapping("delhistory")
  @ResponseBody
  public ServerResponse delHistory(HttpSession session){
    try{
      String history = "history";
      session.removeAttribute(history);
      return ServerResponse.createSuccess();
    }catch (Exception e){
      e.printStackTrace();
      return ServerResponse.createError();
    }
  }

  @RequestMapping(value = "/fuzzysearch",method = RequestMethod.POST)
  public ModelAndView fuzzy(String fuzzy,HttpSession session,
                            @RequestParam(value = "pageid", required = false, defaultValue = "1") int pageid) {
    return service.initFuzzyList(fuzzy,session,pageid,COUNT);
  }

  @RequestMapping(value = "updateCollect",method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse updateCollect(String pid, HttpSession session, HttpServletRequest request) {
    User user = (User) session.getAttribute("user");
    if (user == null) {
      String msg = "未检测到用户，请先登录";
      String address = request.getContextPath() + "/login";
      Msg ms = new Msg(msg, address);
      return ServerResponse.createError(ms);
    }
    return service.updateCollect(pid, user);
  }

  @RequestMapping(value = "Collect")
  public ModelAndView goCollect(HttpSession session){
    User user = getSessionUser(session);
    ModelAndView mv = new ModelAndView("user/login");
    if(user != null){
      List<Product> myCollect = service.findMyCollect(user.getUid());
      mv.addObject("collect",myCollect);
      mv.setViewName("product/collect");
    }
    return mv;
  }
//  ----------------------------------评论--------------------------------------

}
